<?php
/*
 * @Author: 李志刚
 * @CopyRight: 2022-2099 李志刚（854378082@qq.com）
 * @Date: 2018-07-25 11:39:58
 * @Description: 广告管理
 * @LastEditors: 李志刚
 * @LastEditTime: 2022-04-22 11:37:34
 * @FilePath: /CoinCMF/app/Http/Controllers/Console/Content/AdController.php
 */


namespace App\Http\Controllers\Console\Content;

use App\Models\Content\Ad;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\BatchRequest;
use App\Http\Requests\Cms\AdRequest;
use App\Http\Requests\Cms\AdUpdateRequest;
use App\Http\Requests\DetailRequest;
use App\Http\Requests\SortRequest;

class AdController extends Controller
{
    
    public function getList(Request $request)
    {
        $key = $request->input('key', '');
        $starttime = $request->input('starttime', '');
        $endtime = $request->input('endtime', '');
        $status = $request->input('status', '');
        $page = $request->input('page', 1);
        $size = $request->input('size', 10);
        $list = Ad::with('ad_pos')->where(function ($q) use ($key) {
            if ($key != '') {
                $q->where('title', 'like', '%' . $key . '%');
            }
        })->where(function ($q) use ($starttime, $endtime) {
            if ($starttime != '' && $endtime != '') {
                $q->where('created_at', '>=', $starttime)->where('created_at', '<=', $endtime);
            }
        })->where(function ($q) use ($status) {
            if ($status != '') {
                $q->where('status', $status);
            }
        })->where('del_flag', 0)->limit($size)->offset(($page - 1) * $size)->orderBy('id', 'desc')->get();
        $count = Ad::where(function ($q) use ($key) {
            if ($key != '') {
                $q->where('title', 'like', '%$key%');
            }
        })->where(function ($q) use ($starttime, $endtime) {
            if ($starttime != '' && $endtime != '') {
                $q->where('created_at', '>=', $starttime)->where('created_at', '<=', $endtime);
            }
        })->where(function ($q) use ($status) {
            if ($status != '') {
                $q->where('status', $status);
            }
        })->where('del_flag', 0)->count();
        return $this->resJson(200, '成功', ['list' => $list, 'count' => $count]);
    }
    public function postCreate(AdRequest $request)
    {
        $data = $request->all();
        $create = ['pos_id' => $data['pos_id'], 'title' => $data['title'], 'thumb' => $data['thumb'], 'url' => $data['url'], 'sort' => $data['sort'], 'status' => $data['status']];
        $create['starttime'] = $data['starttime'] == '' ? date('Y-m-d H:i:s') : $data['starttime'];
        $create['endtime'] = $data['endtime'] == '' ? date('Y-m-d H:i:s') : $data['endtime'];
        Ad::create($create);
        return $this->resJson(200, '成功');
    }
    public function getDetail(DetailRequest $request)
    {
        $detail_id = $request->input('detail_id');
        $detail = Ad::findOrFail($detail_id);
        return $this->resJson(200, '成功', $detail);
    }
    public function postEdit(AdUpdateRequest $request)
    {
        $data = $request->all();
        $detail_id = $data['detail_id'];
        $update = ['pos_id' => $data['pos_id'], 'title' => $data['title'], 'thumb' => $data['thumb'], 'url' => $data['url'], 'sort' => $data['sort'], 'status' => $data['status']];
        $update['starttime'] = $data['starttime'] == '' ? date('Y-m-d H:i:s') : $data['starttime'];
        $update['endtime'] = $data['endtime'] == '' ? date('Y-m-d H:i:s') : $data['endtime'];
        Ad::where('id', $detail_id)->update($update);
        return $this->resJson(200, '成功');
    }
    public function postRemove(BatchRequest $request)
    {
        $detail_id = $request->input('detail_id',[]);
        Ad::whereIn('id', $detail_id)->update(['del_flag' => 1]);
        return $this->resJson(200, '成功');
    }
    public function postSort(SortRequest $request)
    {
        $detail_id = $request->input('detail_id');
        Ad::where('id', $detail_id)->update(['sort' => $request->input('sort',0)]);
        return $this->resJson(200, '成功');
    }
}
